package com.spring.schedule_system.utils;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Map;
@Data
@EqualsAndHashCode(callSuper = false)
public abstract class ExcelAnalysisEventListener<T> extends AnalysisEventListener<T> {

    @ApiModelProperty(value = "excel表头")
    private Map<Integer, String> headMap;

    @ApiModelProperty(value = "读取到第n行数据行 (不包括表头)")
    private int currentRecordIndex;

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        this.headMap = headMap;
        dealHeadMap(this.headMap);
    }

    // 表头回调
    public abstract void dealHeadMap(Map<Integer, String> headMap);

    @Override
    public void invoke(T record, AnalysisContext analysisContext) {
        // 解析每一行数据
        this.currentRecordIndex++;
        // 回调处理行数据
        dealRecord(record);
    }

    // 行数据回调
    public abstract void dealRecord(T record);

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        // 读取数据完毕回调
        readEnd();
    }

    // 行数据回调
    public abstract void readEnd();


}
